#ifndef OHOS_VIDEO_RENDER_BASE_H_
#define OHOS_VIDEO_RENDER_BASE_H_

#include <mutex>
#include "ohos_egl_render.h"
#include "ohos_video_frame_drawer.h"
#include "api/video/video_frame.h"
#include "api/video/video_scale_mode.h"
#include "api/video/video_render_interface.h"
#include "common_video/util/matrix.h"
#include "modules/video_render/video_size_measure.h"
#include "api/video/video_render_interface.h"
namespace webrtc {
class BaseVideoRender : public VideoRenderInterface {
 public:
  BaseVideoRender();
  virtual ~BaseVideoRender() = default;
  
  void AttachHandle(void* handle) override { }
  std::string GetRenderId() const override { return ""; }
  
  bool Init(void *window, void* share_context, int width, int height) override;
  void SizeChange(int width, int height) override;
  void ClearColor(const std::vector<float> &rgba_color) override;
  void Release() override;
  void SetScaleType(webrtc::VideoScaleMode h_match_scale_type, webrtc::VideoScaleMode h_mismatch_scale_type, webrtc::VideoScaleMode v_match_scale_type, webrtc::VideoScaleMode v_mismatch_scale_type) override;
  void SetMirror(bool mirror) override;
  bool GetMirror() const override;
  
  void OnFrame(const webrtc::VideoFrame& frame) override;

  void AddRenderListener(std::shared_ptr<VideoRenderListener> listener) override;
  void RemoveRenderListener(std::shared_ptr<VideoRenderListener> listener) override;

 protected:
  std::shared_ptr<EGLRender> egl_render_;
  std::shared_ptr<VideoFrameDrawer> frame_drawer_;
  std::shared_ptr<VideoSizeMeasure> video_size_measure_;
  std::shared_ptr<Matrix> draw_matrix_;
  std::vector<std::shared_ptr<VideoRenderListener>> render_listeners_;

  std::string tid_;
  bool mirror_;
  int surface_width_ = 0;
  int surface_height_ = 0;
  int render_width_ = 0;
  int render_height_ = 0;
  int layout_width_ = 0;
  int layout_height_ = 0;
  int frame_width_ = 0;
  int frame_height_ = 0;
  int frame_rotation_ = 0;
  float layout_aspect_ratio_ = 0;

  bool is_first_frame_rendered_;
  void UpdateLayoutAspectRatio();
  void UpdateFrameDimensions(int frame_width, int frame_height, int frame_rotation);
  
 private:
     std::mutex layout_lock_;
     std::string GetTid() const;
     void CalculateDrawProperties(int frame_width, int frame_height, int frame_rotation, int &viewport_x, int &viewport_y,
                                  int &viewport_width, int &viewport_height);
     void UpdateSurfaceSize(int width, int height);
};
}
#endif  // OHOS_VIDEO_RENDER_BASE_H_